﻿#include<iostream>
#include<cstdarg>
using namespace std;

struct SArg {
	int count;//参数个数
	char* cMem;
};

int AverAge(short count, ...) {
	va_list arg;//va_list是char类型的指针
	char* arg1;

	va_start(arg, count);//得到参数个数

	cout << "start:" << hex << (int)arg << endl;

	int x = va_arg(arg, int);//读取一个参数
	cout << x << endl;

	x = va_arg(arg, int);//读取下一个参数
	cout << x << endl;

	x = va_arg(arg, int);//读取下一个参数
	cout << x << endl;

	int sum{};

	for (int i{}; i < count; i++) {
		cout << "arg:" << (int)arg << endl;//每次递增8，代表每执行一次va_arg就会先读取再向上递增一次，读取下一个数
		sum += va_arg(arg, int);
	}

	sum = sum / count;

	va_end(arg);//释放指针

	return sum;
}

int AverAge(SArg& y) {
	int sum{};
	int* ary = (int*)y.cMem;
	for (int i = 0; i < y.count; i++) {
		sum += ary[i];
	}
	return sum / y.count;
}

int main() {
	int x = AverAge(3, 53, 465, 11);//第一个数为后面不定量参数的个数，可以传入不同类型的参数，能读就行
	cout << "平均数" << x << endl;

	SArg y;
	y.count = 5;
	y.cMem = (char*) new int[5] {1, 2, 3, 4, 5};
	int z = AverAge(y);
	cout << "平均数" << z << endl;
}